/**
 *
 */
package com.zhuoyue.elmt;

import com.eit.eitsite.common.service.BaseService;
import com.eit.eitsite.common.utils.CacheUtils;
import com.eit.eitsite.common.utils.SpringContextHolder;
import com.eit.eitsite.modules.sys.dao.*;
import com.eit.eitsite.modules.sys.entity.*;
import com.eit.eitsite.modules.sys.security.SystemAuthorizingRealm.Principal;
import com.eit.eitsite.modules.sys.utils.UserUtils;
import com.zhuoyue.elmt.dao.*;
import com.zhuoyue.elmt.entity.FailureReport;
import com.zhuoyue.elmt.entity.GgZgdw;
import com.zhuoyue.elmt.entity.MaintenanceStaff;
import com.zhuoyue.elmt.service.GgZgdwService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;

public class UserExtUtils {

    private static FailureReportDao failureReportDao = SpringContextHolder.getBean(FailureReportDao.class);
    private static MaintenanceRecordSzDao maintenanceRecordSzDao = SpringContextHolder.getBean(MaintenanceRecordSzDao.class);
    private static StatisticsDao statisticsDao = SpringContextHolder.getBean(StatisticsDao.class);
    private static MaintenanceStaffDao maintenanceStaffDao = SpringContextHolder.getBean(MaintenanceStaffDao.class);
    private static GgZgdwDao ggZgdwDao = SpringContextHolder.getBean(GgZgdwDao.class);
    private static DictDao dictDao = SpringContextHolder.getBean(DictDao.class);

    public static final String USER_CACHE = "userExtCache";
    public static final String USER_CACHE_ID_ = "id_";
    public static final String USER_CACHE_LOGIN_NAME_ = "ln";

    // 累计故障次数
    public static String statFailureReport(String loginName) {
        FailureReport qryFailureReport = new FailureReport();
        qryFailureReport.setCreatedBy(loginName);
        List<FailureReport> retList = failureReportDao.findList(qryFailureReport);

        int statCount = 0;
        if (retList != null) {
            statCount = retList.size();
        }

        return String.valueOf(statCount);
    }

    // 比例权重可以配置
    // (累计故障次数/设备数) * 50 + ((逾期数 + 维保记录不合格）/维保人员的维保记录的记录数） * 50
    // 故障来源维保记录要剔除
    // 我的评分
    public static String getScore(String loginName) {
        HashMap paramMap = new HashMap();
        User user = UserUtils.getByLoginName(loginName);

        Dict qryDict = new Dict();
        qryDict.setType("STAT_WEIGHT");
        List<Dict> dictList = dictDao.findList(qryDict);
        String failureWeight = "50";
        String mainWeight = "50";
        for (Dict dict : dictList) {
            if ("1".equals(dict.getValue())) {
                failureWeight = dict.getLabel();
            }
            if ("2".equals(dict.getValue())) {
                mainWeight = dict.getLabel();
            }
        }
        paramMap.put("failureWeight", failureWeight); // 故障权重
        paramMap.put("mainWeight", mainWeight); // 维保权重

        paramMap.put("staff", user.getLoginName());
        paramMap.put("wbdwdm", user.getCompany().getCode());
        List<HashMap> retList = statisticsDao.statrank(paramMap);

        // 通过排行榜数据反推分数
        Double statCount = new Double(0);
        if (retList != null && retList.size() > 0) {
            HashMap retMap = retList.get(0);
            statCount = (Double) retMap.get("statcount");
        }

        if (statCount == null) {
            return "0";
        } else {
            return String.valueOf(statCount.intValue());
        }
    }

    public static MaintenanceStaff getMaintenanceStaff(String loginName) {
        return maintenanceStaffDao.getByLoginName(loginName);
    }

    public static void updateMaintenanceStaff(MaintenanceStaff maintenanceStaff) {
        maintenanceStaffDao.update(maintenanceStaff);
    }

    public static GgZgdw getGgZgdw(String zzjqdm) {
        GgZgdw qryGgZgdw = new GgZgdw();
        qryGgZgdw.setZzjqdm(zzjqdm);
        return ggZgdwDao.getByCode(qryGgZgdw);
    }

}
